﻿package at.geoathome.utils
{
	import flash.display.DisplayObject;
	
	/**
	* @author Georg Kamptner
	* FlashVars is implemented as Singleton (with the exception that the getInstance() Method is not public visible)
	* toDo: - finish
	*/
	public class FlashVars 
	{
		private var _flash_vars:Object;
		private var _crucial_flash_vars:Array;
		private var _extracted:Boolean;
		
		private static var _instance:FlashVars;
		
		public function FlashVars(singleton_enforcer:SingletonEnforcer) 
		{
			_flash_vars = new Object();
			_crucial_flash_vars = new Array();
			_extracted = false;
		}
		
		private static function getInstance():FlashVars
		{
		  if (_instance == null)
			_instance = new FlashVars(new SingletonEnforcer());
			
		  return _instance;
		}
		
		private function _getFlashVar(name:String):String
		{
			return _getFlashVarAsString(name);
		}
		
		private function _getFlashVarAsString(name:String):String
		{
			return _flash_vars[name] as String;
		}
		
		private function _getFlashVarAsInt(name:String):int
		{
			if (_flash_vars[name] == undefined)
				return NaN;
				
			return parseInt(_flash_vars[name]);
		}
		
		private function _getFlashVarAsNumber(name:String):Number
		{
			if (_flash_vars[name] == undefined)
				return NaN;

			return parseFloat(_flash_vars[name]);
		}
		
		private function _getFlashVarAsBool(name:String):Boolean
		{
			if (_flash_vars[name] == undefined)
				return false;
			
			var var_as_string:String = _flash_vars[name] as String;
			
			if (var_as_string.length == 1)
			{
				if (var_as_string == "0")
					return false;
				else
					return true;
			}
			else
			{
				if (var_as_string.toLowerCase() == "false")
					return false;
				else
					return true;
			}
		}
		
		private function _extractFlashVars(root:DisplayObject):void
		{
			if (root == null)
				throw new Error("Flashvars extraction impossible: No root.loaderInfo available!");
				
			_flash_vars = root.loaderInfo.parameters;
			_extracted = true;
			checkCrucialVars();
		}
		
		private function checkCrucialVars():void
		{
			// check only of vars havee allready been extracted
			if (!_extracted)
				return;
				
			// check if all flash vars are available
			var l:uint = _crucial_flash_vars.length;
			var i:uint;
			for (i = 0; i<l; ++i)
			{
				if (_flash_vars[_crucial_flash_vars[i]] == undefined)
				{
					throw new Error("Flashvar: '" + _crucial_flash_vars[i] + "' missing.");
					break;
				}
			}
		}
		
		private function _setCrucialFlashVars(crucial_flash_vars:Array):void
		{
			_crucial_flash_vars = crucial_flash_vars;
			checkCrucialVars();
		}
		
		private function toString():String
		{
			var str:String = "";
			str += "\n [com.kronomy.utils.FlashVars] \n";
			
			return str;
		}
		
		// - STATIC API -----------------------------------------------------------------------------
		public static function getFlashVar(name:String):String {
			return FlashVars.getInstance()._getFlashVar(name);
		}
		
		public static function getFlashVarAsString(name:String):String {
			return FlashVars.getInstance()._getFlashVarAsString(name);
		}
		
		public static function getFlashVarAsInt(name:String):int {
			return FlashVars.getInstance()._getFlashVarAsInt(name);
		}
		
		public static function getFlashVarAsNumber(name:String):Number {
			return FlashVars.getInstance()._getFlashVarAsNumber(name);
		}
		
		public static function getFlashVarAsBool(name:String):Boolean {
			return FlashVars.getInstance()._getFlashVarAsBool(name);
		}
		
		public static function extractFlashVars(root:DisplayObject):void {
			FlashVars.getInstance()._extractFlashVars(root);
		}
		
		public static function setCrucialFlashVars(crucial_flash_vars:Array):void {
			FlashVars.getInstance()._setCrucialFlashVars(crucial_flash_vars);
		}
		
		
		
	}
	
}

class SingletonEnforcer {}